package com.program.beautymakeup.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.program.beautymakeup.domain.bo.UserQueryBO;
import com.program.beautymakeup.domain.entity.User;
import com.program.beautymakeup.domain.vo.PageVO;
import com.program.beautymakeup.domain.vo.ResultInfo;
import com.program.beautymakeup.domain.vo.UserVO;
import com.program.beautymakeup.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author CaiKe
 * @date 2023/2/8 21:33
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UserService {

    private final UserMapper userMapper;

    public List<User> userList() {
        return userMapper.selectAll();
    }

    public User getUserInfo(Integer userId) {
        return userMapper.selectById(userId);
    }


    public int countUserByUsername(String username){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        return userMapper.selectCount(queryWrapper);
    }

    public User login(String username, String password){
        boolean success = userMapper.countByUsernameAndPassword(username, password) > 0;
        if(success){
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("username",username);
            return userMapper.selectList(queryWrapper).stream().findFirst().get();
        }else return null;
    }

    public void saveUser(User user){
        userMapper.insert(user);
    }

    public void deleteUserById(Integer id) {
        userMapper.deleteById(id);
    }

    public boolean updateUser(User userToSave) {
        return userMapper.updateById(userToSave)>0;
    }

    public PageVO<UserVO> queryList(UserQueryBO userQueryBO) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if(userQueryBO.getUserId() != null) queryWrapper.eq("user_id",userQueryBO.getUserId());
        if(userQueryBO.getUsername() != null) queryWrapper.likeRight("username",userQueryBO.getUsername());
        if(userQueryBO.getRealname() != null) queryWrapper.likeRight("realname",userQueryBO.getRealname());
        if(userQueryBO.getRole() != null) queryWrapper.eq("role",userQueryBO.getRole());
        Page<User> page = new Page<>(userQueryBO.getCurrent(),userQueryBO.getPageSize());
        IPage<User> iPage = userMapper.selectPage(page,queryWrapper);
        PageVO<UserVO> pageVO = new PageVO<>();
        pageVO.setPages(iPage.getPages());
        pageVO.setCurrent(iPage.getCurrent());
        pageVO.setSize(iPage.getSize());
        pageVO.setTotal(iPage.getTotal());
        pageVO.setRecords(iPage.getRecords().stream().map((user)->{
            UserVO userVO = new UserVO();
            userVO.setUserId(user.getUserId());
            userVO.setAvatar(user.getAvatar());
            userVO.setContent(user.getContent());
            userVO.setMobile(user.getMobile());
            userVO.setUsername(user.getUsername());
            userVO.setRealname(user.getRealname());
            userVO.setRole(user.getRole());
            userVO.setSignature(user.getSignature());
            userVO.setWx(user.getWx());
            return userVO;
        }).collect(Collectors.toList()));
        return pageVO;
    }
}